import pymysql


def get_conn():
    # 连接MySQL数据库
    return pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='123456',
        db='test',
        charset='utf8mb4'
    )


def exec(conn, sql):
    # 创建游标对象
    cursor = conn.cursor()
    # 执行SQL语句
    cursor.execute('select * from user')
    # 获取查询结果
    result = cursor.fetchall()
    # 输出查询结果
    print(result)
    # 关闭游标对象
    cursor.close()


def close_conn(conn):
    conn.close()
